package cn.com.cym.gamezao.web.authorize;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.base.SearchPage;
import cn.com.cym.gamezao.entity.authorize.UserRole;
import cn.com.cym.gamezao.service.authorize.UserRoleService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.web.bind.annotation.*;

@Api(tags = "a-用户角色")
@RestController
@RequestMapping(value = "userRole")
public class UserRoleController {

    final UserRoleService thisService;

    public  UserRoleController(UserRoleService userRoleService){
        this.thisService=userRoleService;
    }

    @ApiImplicitParam(name = "id", value = "id", required = true,dataType = "Long")
    @ApiOperation(value = "按照id获取")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping("/{id}")
    @AuthToken(rule = "authorize.userRole.search")
    public ResponseEntity<?> getById(@PathVariable @NonNull Long id){
        UserRole userRole = this.thisService.fetchOne(id);
        return ResponseEntity.ok(userRole);
    }


    @ApiImplicitParam(name = "userRole", value = "userRole", required = true,dataType = "UserRole")
    @ApiOperation(value = "新建用户角色")
    @ApiOperationSupport(order = 2, author = "mr.chen")
    @PostMapping()
    @AuthToken(rule = "authorize.userRole.add")
    public ResponseEntity<?> add(@RequestBody  UserRole userRole){
        this.thisService.save(userRole);
        return ResponseEntity.ok(userRole);
    }


    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id",paramType = "path", required = true,dataType = "Long"),
            @ApiImplicitParam(name = "userRole", value = "userRole",paramType = "", required = true,dataType = "UserRole")})
    @ApiOperation(value = "更新用户角色")
    @ApiOperationSupport(order = 3, author = "mr.chen")
    @PutMapping("/{id}")
    @AuthToken(rule = "authorize.userRole.update")
    public ResponseEntity<?> update(@RequestBody   UserRole userRole,@PathVariable @NonNull Long id){
        final  UserRole storeUserRole = this.thisService.update(userRole,id);
        return ResponseEntity.ok(storeUserRole);
    }

    @ApiImplicitParam(name = "id", value = "id",paramType = "path",required = true,dataType = "Long")
    @ApiOperation(value = "删除用户角色")
    @ApiOperationSupport(order = 2, author = "mr.chen")
    @DeleteMapping("/{id}")
    @AuthToken(rule = "authorize.userRole.delete")
    public ResponseEntity<?> delete(@PathVariable Long id){
        this.thisService.physicalDelete(id);
        return ResponseEntity.ok(new ResultResponse("删除成功", HttpStatus.OK.value()));
    }

    @ApiImplicitParam(name = "searchPage", value = "searchPage", required = true,dataType = "SearchPage")
    @ApiOperation(value = "分页显示page！")
    @ApiOperationSupport(order = 4, author = "mr.chen")
    @PostMapping("/page")
    @AuthToken
    public ResponseEntity<?> page(@RequestBody SearchPage searchPage){
        Page<?> byPageable = this.thisService.findByPageable(searchPage);
        return ResponseEntity.ok(byPageable);
    }
}
